home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Demo's / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Annotations / Append Fit Equation < prev    next >
Text File  |  1994-02-18  |  3KB  |  132 lines

  1. #include <Value Report>
  2.  
  3. |************
  4. |    Procs to support AppendFitEquation() and the test routines...
  5. |************
  6.  
  7. |************
  8. | Returns string containing a built-in curve fit equation
  9. |  fitType is an integer selector
  10. Function/S GenFitEquation(fitType,nterms)
  11.     variable fitType                | 1= Gaussian, 2= Lorentzian etc
  12.     variable nterms                | need not be valid except for poly
  13.  
  14.     if( (fitType<1) %| (fitType>7) )
  15.         return "GenFitEquation: unknown fit type"
  16.     endif
  17.  
  18.     if(fitType==1)
  19.         return "K0+K1*exp(-((x-K2)/K3)^2)"
  20.     endif
  21.     if(fitType==2)
  22.         return "K0+K1/((x-K2)^2+K3)"
  23.     endif
  24.     if(fitType==3)
  25.         return " K0+K1*exp(-K2*x)"
  26.     endif
  27.     if(fitType==4)
  28.         return " K0+K1*exp(-K2*x)+K3*exp(-K4*x)"
  29.     endif
  30.     if(fitType==5)
  31.         return " K0+K1*sin(K2*x+K3)"
  32.     endif
  33.     if(fitType==6)
  34.         return " K0+K1*x"
  35.     endif
  36.     
  37.     String s= " K0+K1*x"
  38.     if(fitType==7)
  39.         if( (nterms<3) %| (nterms>20) )
  40.             return "GenFitEquation: bad number of terms for poly eqn"
  41.         endif
  42.         string t
  43.         variable i=2
  44.         do
  45.             sprintf t,"+K%d*x^%d",i,i
  46.             s += t
  47.             i +=1
  48.         while(i<nterms)
  49.     endif
  50.     return s
  51. End
  52.  
  53. |************
  54. | Generic substring replacement routine. 
  55. |
  56. Function/S SubStrReplace(theStr,beforeSub,afterSub,caseSensitive)
  57.     string theStr                    | string to do the replacement on
  58.     string beforeSub                | substring in theStr to replace by ...
  59.     string afterSub                | ... this string
  60.     variable caseSensitive        | non-zero if case sensitive search is desired
  61.  
  62.     string tmp= theStr            | working copy
  63.     variable start                    | start of substring
  64.     
  65.     if(!caseSensitive)
  66.         tmp= UpperStr(tmp)
  67.         beforeSub= UpperStr(beforeSub)
  68.     endif
  69.     start= strsearch(tmp,beforeSub,0)
  70.     if( start >= 0 )
  71.         theStr[start,start+strlen(beforeSub)-1]= afterSub
  72.     endif
  73.     return theStr
  74. End
  75.  
  76.  
  77.  
  78. |************
  79. | Assuming the top graph contains data that has just been fitted to a built-in function
  80. |  this macro adds or modifies a text box containing the fitted equation using
  81. |  the actual values from the fit.  The values are rounded depending on the estimated errors.
  82. |  The textbox is given the name 'fiteqn'. 
  83. |  If you want an extra digit of precision shown change vrsFlags to 2 (see below)
  84. |
  85. | REQUIRES: IGOR V1.2; MakeValueReportString();GenFitEquation();SubStrReplace()
  86. |    
  87. Macro AppendFitEquation(fitType)
  88.     variable fitType=1
  89.     Prompt fitType,"Fit type:",popup,"gauss;lor;exp;dblexp;sine;line;poly"
  90.  
  91.     Silent 1
  92.     
  93.     if( (fitType<1) %| (fitType>7) )
  94.         Abort "unknown fit type"
  95.     endif
  96.     
  97.     String tmpText
  98.     String afestr_tmp,afestr_tmp2
  99.     Variable nterms= numpnts(W_sigma),start
  100.     Variable vrsFlags= 4+2                    | 1 digit, use E notation
  101.     afestr_tmp= GenFitEquation(fitType,nterms)
  102.     
  103.     if( fitType==6 )        | fit to straight line is special...
  104.         Make/O W_sigma={V_siga,V_sigb}
  105.         nterms= 2
  106.     endif
  107.     Iterate( nterms )
  108.         tmpText= "K"+num2istr(i)
  109.         afestr_tmp2= MakeValueReportString(W_coef[i],W_sigma[i],0,"",vrsFlags)
  110.         afestr_tmp= SubStrReplace(afestr_tmp,tmpText,afestr_tmp2,0)
  111.     loop
  112.     | may have '+-' and/or '--' as a result of the direct substitution
  113.     start= 0
  114.     do
  115.         start= strsearch(afestr_tmp,"+-",start)
  116.         if(start<0)
  117.             break;
  118.         endif
  119.         afestr_tmp[start,start+1]= "-"
  120.     while(1)
  121.     start= 0
  122.     do
  123.         start= strsearch(afestr_tmp,"--",start)
  124.         if(start<0)
  125.             break;
  126.         endif
  127.         afestr_tmp[start,start+1]= "-"
  128.     while(1)
  129.     Textbox/C/N=fiteqn afestr_tmp
  130. End
  131.  
  132.